﻿using System.Collections.Generic;
using System.Web.Caching;
using System.Configuration;

namespace Common.Cache
{
    public static class SqlCacheDependencyExtension
    {
        public static CacheDependency GetSqlCacheDependency(this SqlCacheDependency sqlCacheDependency, IEnumerable<string> tableNames, string databaseName)
        {
            AggregateCacheDependency dependency = new AggregateCacheDependency();
            foreach (string tableName in tableNames)
            {
                dependency.Add(new SqlCacheDependency(databaseName, tableName));
            }
            return dependency;
        }

        public static CacheDependency GetSqlCacheDependency(this SqlCacheDependency sqlCacheDependency, string configTableKey, string configDatabaseKey)
        {
            AggregateCacheDependency dependency = new AggregateCacheDependency();
            string dbName = ConfigurationManager.AppSettings[configDatabaseKey];
            string tableConfig = ConfigurationManager.AppSettings[configTableKey];
            string[] tables = tableConfig.Split(',');

            foreach (string tableName in tables)
            {
                dependency.Add(new SqlCacheDependency(dbName, tableName));
            }
            return dependency;
        }
    }
}
